Previous Book Contents Book Index Next

Inside Macintosh: Overview /
Chapter 6 - Windows


Closing Windows

The user closes a window either by clicking the window's close box (in the upper-left corner of the window) or by choosing the Close command from the File menu. To determine which window to close, you'll proceed in slightly different ways for these two cases. When the user clicks a window's close box, you can get a window pointer for that window by calling the FindWindow function in response to the mouse-down event. When the user chooses a menu command, however, you can't do that; instead, you can call the FrontWindow function to retrieve a pointer to the frontmost window on the screen.

Note
You'll also want to close any windows that might be on the desktop when the user quits your application. You can do that by repeatedly calling FrontWindow until it returns NIL. See Listing 9-4 on page 175.
When the user presses the mouse button while the cursor is in the close box, your application should call the TrackGoAway function to track mouse movement until the user releases the button, as illustrated in Listing 6-13.

Listing 6-13 Handling clicks in the close box

PROCEDURE DoGoAwayBox (myWindow: WindowPtr; mouseloc: Point);
BEGIN
   IF TrackGoAway(myWindow, mouseloc) THEN
      DoCloseWindow(myWindow);
END;
If TrackGoAway returns FALSE, the user released the button while the cursor was outside the close box, and your application should do nothing. If TrackGoAway returns TRUE, your application should invoke its own procedure for closing a window.

Listing 6-14 illustrates an application-defined function that closes a window. Notice that the effect of this function varies according to which kind of window it's being asked to close. If the user wants to close a dialog window, DoCloseWindow simply hides the window; this strategy leaves the data structures associated with the dialog box in memory, in expectation that the user might open the dialog box again. If the user wants to close a desk accessory window, DoCloseWindow calls the Desk Manager routine CloseDeskAcc to close that desk accessory.

Listing 6-14 Closing a window

PROCEDURE DoCloseWindow (myWindow: WindowPtr);
BEGIN
   IF myWindow <> NIL THEN
      IF IsDialogWindow(myWindow) THEN       {this is a dialog window}
         HideWindow(myWindow)
      ELSE IF IsDAccWindow(myWindow) THEN    {this is a DA window}
         CloseDeskAcc(WindowPeek(myWindow)^.windowKind)
      ELSE IF IsAppWindow(myWindow) THEN     {this is a document window}
         DoCloseDocWindow(myWindow);
END;
If the window to be closed is a document window, DoCloseWindow calls the application-defined procedure DoCloseDocWindow defined in Listing 6-15 to deallocate the document record, close the window, and then deallocate the window record.

Listing 6-15 Closing a Venn diagram window

PROCEDURE DoCloseDocWindow (myWindow: WindowPtr);
   VAR
      myHandle:   MyDocRecHnd;
BEGIN
   IF myWindow = NIL THEN
      exit(DoCloseDocWindow)              {ignore NIL windows}
   ELSE
      BEGIN
         myHandle := MyDocRecHnd(GetWRefCon(myWindow));
         DisposeHandle(Handle(myHandle));
         CloseWindow(myWindow);           {close the window}
         DisposePtr(Ptr(myWindow));       {and release the storage}
      END;
END;
The DoCloseDocWindow procedure retrieves a handle to the document record from the window record. Then it calls DisposeHandle to free the memory occupied by the document record. Next DoCloseDocWindow closes the window by calling the Window Manager procedure CloseWindow and deallocates the window record by calling DisposePtr.

Note
When you create a window, if you allow the Window Manager to allocate memory for the window record (by passing NIL as the second parameter to GetNewWindow), then you should call the DisposeWindow procedure to close the window, instead of calling CloseWindow and DisposePtr.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
9 JUL 1996